package com.work.keep.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.work.keep.R;
import com.work.keep.adapter.CollectionAdapter;
import com.work.keep.base.BaseActivity;
import com.work.keep.entity.dao.Collection;
import com.work.keep.entity.dao.CollectionDao;
import com.work.keep.entity.dao.DaoSingleton;
import com.work.keep.ui.PullToRefreshRecyclerView;

import java.util.List;

/**
 * Created by dllo on 16/1/26.
 */
public class CollectionActivity extends BaseActivity implements View.OnClickListener {
    private PullToRefreshRecyclerView pullToRefreshRecyclerView;
    private RecyclerView recyclerView;
    private ImageView collectionLeft;
    private CollectionAdapter collectionAdapter;
    private List<Collection> collectionList;
    private CollectionDao collectionDao;

    @Override
    protected View getLayoutView() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_collection;
    }

    @Override
    protected void initView() {
        pullToRefreshRecyclerView = (PullToRefreshRecyclerView) findViewById(R.id.collectionPullToRefresh);
        recyclerView = (RecyclerView) pullToRefreshRecyclerView.getRefreshableView();
        collectionLeft = (ImageView) findViewById(R.id.collectionLeft);
    }

    @Override
    protected void initData() {
        collectionLeft.setOnClickListener(this);
        collectionDao = DaoSingleton.getInstance().getCollectionDao();
        collectionList = collectionDao.queryBuilder().list();
        collectionAdapter = new CollectionAdapter(collectionList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(collectionAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.collectionLeft:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        //用户退出界面时做判断 对数据库进行修改
        List<Integer> deletePositions = collectionAdapter.getDeletePosition();
        for (Integer position : deletePositions
                ) {
            collectionDao.delete(collectionList.get(position));
        }
        super.onDestroy();
    }
}
